/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.khstudio.api.dialog;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.khstudio.api.R;

public class KhDialogSimple extends KhDialog {
    private String              mBody;
    private String              mPositive;
    private String              mNegative;
    private String              mCancel;

    public static KhDialogSimple show(Activity a,
                                      int id, int titleResId,
                                      int bodyResId,
                                      int positiveResId,
                                      int negativeResId,
                                      int cancelResId) {
        FragmentManager     fm = a.getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        Fragment            prev = fm.findFragmentByTag("khdialogsimple");

        Resources           r = a.getResources();

        if (prev != null) {
            ft.remove(prev);
        }

        Bundle args = new Bundle();
        args.putInt("id", id);

        if(titleResId > 0) {
            args.putString("title", r.getString(titleResId));
        }
        if(bodyResId > 0) {
            args.putString("body", r.getString(bodyResId));
        }
        if(positiveResId > 0) {
            args.putString("positive", r.getString(positiveResId));
        }
        if(negativeResId > 0) {
            args.putString("negative", r.getString(negativeResId));
        }
        if(cancelResId > 0) {
            args.putString("cancel", r.getString(cancelResId));
        }

        // Create and show the dialog.
        KhDialogSimple f = new KhDialogSimple();
        f.setArguments(args);
        f.show(ft, "khdialogsimple");

        return f;
    }

    public static KhDialogSimple show(FragmentManager fm,
                                      int id, String title,
                                      String body,
                                      String positive,
                                      String negative,
                                      String cancel) {
        FragmentTransaction ft = fm.beginTransaction();
        Fragment prev = fm.findFragmentByTag("khdialogsimple");

        if (prev != null) {
            ft.remove(prev);
        }

        Bundle args = new Bundle();
        args.putInt("id", id);
        if(title != null) {
            args.putString("title", title);
        }
        if(body != null) {
            args.putString("body", body);
        }
        if(positive != null) {
            args.putString("positive", positive);
        }
        if(negative != null) {
            args.putString("negative", negative);
        }
        if(cancel != null) {
            args.putString("cancel", cancel);
        }

        // Create and show the dialog.
        KhDialogSimple f = new KhDialogSimple();
        f.setArguments(args);
        f.show(ft, "khdialogsimple");

        return f;
    }

    public KhDialogSimple() {
        super();

        mBody     = null;
        mPositive = null;
        mNegative = null;
        mCancel   = null;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mBody     = getArguments().getString("body", null);
        mPositive = getArguments().getString("positive", null);
        mNegative = getArguments().getString("negative", null);
        mCancel   = getArguments().getString("cancel", null);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if(mTitle != null) {
            getDialog().setTitle(mTitle);
        }

        return _createViewSimple(getActivity());
    }

    private View _createViewSimple(Context context) {
        float dip = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getActivity().getResources().getDisplayMetrics());

        LinearLayout ll = new LinearLayout(context);
        ll.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        ll.setOrientation(LinearLayout.VERTICAL);
        ll.setGravity(Gravity.CENTER_HORIZONTAL);
        ll.setPadding((int) (10 * dip), (int) (15 * dip), (int) (10 * dip), (int) (4 * dip));

        if(mBody != null) {
            TextView tv = new TextView(context);
            tv.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            tv.setGravity(Gravity.CENTER);
            tv.setTextAppearance(getActivity(), android.R.style.TextAppearance_DeviceDefault_Medium);
            tv.setText(mBody);
            tv.setVisibility(View.VISIBLE);
            ll.addView(tv);
        }

        ll.addView(_createButton(context));

        return ll;
    }

    private View _createButton(Context context) {
        float dip = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getActivity().getResources().getDisplayMetrics());
        Button btn = null;

        LinearLayout ll = new LinearLayout(getActivity());
        ll.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        ll.setOrientation(LinearLayout.HORIZONTAL);
        ll.setPadding(0, (int) (15 * dip), 0, 0);

        if(mPositive != null) {
            btn = new Button(context);
            btn.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1f));
            btn.setBackgroundResource(R.drawable.kh_btn_selector);
            btn.setText(mPositive);
            btn.setVisibility(View.VISIBLE);
            btn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mListener != null) mListener.onPositiveClick(mId);
                }
            });
            ll.addView(btn);
        }

        if(mNegative != null) {
            btn = new Button(context);
            btn.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1f));
            btn.setBackgroundResource(R.drawable.kh_btn_selector);
            btn.setText(mNegative);
            btn.setVisibility(View.VISIBLE);
            btn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mListener != null) mListener.onNegativeClick(mId);
                }
            });
            ll.addView(btn);
        }

        if(mCancel != null) {
            btn = new Button(context);
            btn.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1f));
            btn.setBackgroundResource(R.drawable.kh_btn_selector);
            btn.setText(mCancel);
            btn.setVisibility(View.VISIBLE);
            btn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mListener != null) mListener.onCancelClick(mId);
                }
            });
            ll.addView(btn);
        }

        return ll;
    }
}
